﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using AmiangDAL;
using AmiangDTO;

namespace AmiangBLL
{
    public class bllBienDongGiaCa
    {
        public static BanHang2DataContext dbs = new BanHang2DataContext();

        public DataTable ConvertListToDatatable(List<BIENDONGGIACA> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaMH");
            dt.Columns.Add("ThoiGian");
            dt.Columns.Add("DonGiaBanSi");
            dt.Columns.Add("DonGiaBanLe");
            foreach(var item in list)
            {
                dt.Rows.Add(item.MaMH, item.ThoiGian, item.DonGiaBanSi, item.DonGiaBanLe);
            }
            return dt;
            
        }
        
        public DataTable GetDataTableNormally()
        {
            var item = from n in dbs.BIENDONGGIACAs
                       select n;
            DataTable dt = new DataTable();
            dt = ConvertListToDatatable(item.ToList<BIENDONGGIACA>());
            return dt;
        }
        
        public DataTable GetDataTableCondition(IQueryable condition)
        {
            var item = condition as IQueryable<BIENDONGGIACA>;
            DataTable dt = new DataTable();
            dt = ConvertListToDatatable(item.ToList<BIENDONGGIACA>());
            return dt;
        }

        public object GetValue(IQueryable condition)
        {
            object item = condition.Cast<object>().First();
            return item;
        }

        public void AddRow(dtoBienDongGiaCa dto)
        {
            BIENDONGGIACA dt = new BIENDONGGIACA();
            dt.MaMH = dto.MaMH;
            dt.ThoiGian = dto.ThoiGian;
            dt.DonGiaBanLe = dto.DonGiaBanLe;
            dt.DonGiaBanSi = dto.DonGiaBanSi;
            if (dt != null)
            {
                dbs.BIENDONGGIACAs.InsertOnSubmit(dt);
                dbs.SubmitChanges();
            }
        }

        public void UpdateRow(dtoBienDongGiaCa dto)
        {
            BIENDONGGIACA dt = new BIENDONGGIACA();
            BIENDONGGIACA item = (from n in dbs.BIENDONGGIACAs
                                  where n.MaMH == dto.MaMH && n.ThoiGian == dto.ThoiGian
                                  select n).Single();
            dt.DonGiaBanSi = dto.DonGiaBanSi;
            dt.DonGiaBanLe = dto.DonGiaBanLe;
            dbs.SubmitChanges();
        }
        
        public void DeleteRow(dtoBienDongGiaCa dto)
        {
            BIENDONGGIACA dt = new BIENDONGGIACA();
            BIENDONGGIACA item = (from n in dbs.BIENDONGGIACAs
                                  where n.MaMH == dto.MaMH && n.ThoiGian == dto.ThoiGian
                                  select n).Single();
            dbs.BIENDONGGIACAs.DeleteOnSubmit(dt);
            dbs.SubmitChanges();
        }
    }
}
